﻿@{    
    CW_Book book = ViewBag.book as CW_Book;
    ViewBag.Title = @Model.ChapterName + "-" + book.BookName;
    ViewBag.keywords = book.BookName + "," + book.BookName + "最新章节," + Model.ChapterName + ",2眼看书网";
    ViewBag.description = "【2眼看书】" + book.BookName + "," + Model.ChapterName;
}
@model CW_BookChapter

<div id="chapterBox">
    <article class="chapter">
        <h1 class="am-text-center chapterTitle">
            @Model.ChapterName
        </h1>
        <div class="am-cf">
            @Html.Raw(Model.ChapterContent)
            
        </div>
    </article>
</div>

@section Scripts{
<script>
    var canLoadChapter = true;
    var nextChapterid = parseInt("@ViewBag.nextid");
    $(function () {
        if (nextChapterid && nextChapterid>0)
        {
            $(window).scroll(function () {
                var scrollVal = $(window).scrollTop();
                var docHigh = $(document).height();
                var winHigh = $(window).height();
                //如果滚动条到底
                if (scrollVal == docHigh - winHigh && canLoadChapter) {
                    loadChapter();
                }
            })
        }
        else
        {
            var newChapter = " <article class=\"chapter\"><h1 class=\"am-text-center chapterTitle\">没有下一章了</h1></article>";
            $("#chapterBox").append(newChapter);
        }

        $(".nav-menu").append(" <a href=\"/book/chapterList/@Model.BookId\"><span class=\"am-icon-list icon\"></span>&nbsp;章节目录</a>")
    })

    function loadChapter() {        
        canLoadChapter = false;
        if (nextChapterid <= 0)
        {
            var newChapter = " <article class=\"chapter\"><h1 class=\"am-text-center chapterTitle\">没有下一章了</h1></article>";
            $("#chapterBox").append(newChapter);
            return;
        }        
        $.ajax({
            type: "POST",
            url: '@Url.Action("GetChapter", "Book", new {bid=Model.BookId })',
            dataType: "json",
            data: 'cid=' + nextChapterid + '&v=' + Math.random(),
            success: function (data) {                
                if (data.chapter)
                {
                    var newChapter = " <article class=\"chapter\">";
                    newChapter += "<h1 class=\"am-text-center chapterTitle\">";
                    newChapter += data.chapter.ChapterName;
                    newChapter += "</h1>";
                    newChapter += "<div class=\"am-cf\">";
                    newChapter += data.chapter.ChapterContent;
                    newChapter += "</div></article>";
                    $("#chapterBox").append(newChapter);

                    //加载后，把此章节加入浏览历史内
                    var chapterHistory = {
                        bookid: data.chapter.BookId,
                        bookname: '@book.BookName',
                        chapterid: data.chapter.Id,
                        chapterName: data.chapter.ChapterName,
                        time: new Date()
                    }
                    //添加到数组开始的位置
                    userHistory.additem(chapterHistory);                    
                }
                nextChapterid = data.nextid;
            },
            beforeSend: function () {
                $("#loading").show();
            },
            complete: function () {
                $("#loading").hide();
                canLoadChapter = true;
            }
        });     
    }   

</script>
}
